﻿// <copyright file="MainPage.xaml.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2010-09-3</date>
// <summary>MainPage code behind</summary>

namespace MostPopular
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.Phone.Controls;
    using NYTimes.Data;

    /// <summary>
    /// MainPage class for Most Popular NYT Articles
    /// </summary>
    public partial class MainPage : PhoneApplicationPage
    {
        /// <summary>
        /// Initializes a new instance of the MainPage class
        /// </summary>
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(this.MainPage_Loaded);
        }

        /// <summary>
        /// Load data for the ViewModel Items
        /// </summary>
        /// <param name="sender">the main page</param>
        /// <param name="e">the routed event arguments</param>
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
        }

        /// <summary>
        /// Navigate to a detail page
        /// </summary>
        /// <param name="sender">the detail button</param>
        /// <param name="e">the mouse button event arguments</param>
        private void DetailButton_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/DetailPage.xaml", UriKind.Relative));
            
            FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
            
            var clickedItem = sender as Border;
            
            App.ViewModel.SelectedItem = clickedItem.DataContext as MostPopularItem;
        }

        /// <summary>
        /// Show the options page
        /// </summary>
        /// <param name="sender">the options button</param>
        /// <param name="e">the event arguments</param>
        private void ShowOptions(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/OptionsPage.xaml", UriKind.Relative));
        }

        /// <summary>
        /// Change the time period to 1 day
        /// </summary>
        /// <param name="sender">the ApplicationBarMenuItem</param>
        /// <param name="e">the event arguments</param>
        private void OneDay_Click(object sender, EventArgs e)
        {
            ////this.OneDay.Text = "● 1 Day";
            ////this.SevenDays.Text = "7 Days";
            ////this.ThirtyDays.Text = "30 Days";
            App.ViewModel.TimePeriod = 1;
        }

        /// <summary>
        /// Change the time period to 7 days
        /// </summary>
        /// <param name="sender">the ApplicationBarMenuItem</param>
        /// <param name="e">the event arguments</param>
        private void SevenDays_Click(object sender, EventArgs e)
        {
            ////this.OneDay.Text = "1 Day";
            ////this.SevenDays.Text = "● 7 Days";
            ////this.ThirtyDays.Text = "30 Days";
            App.ViewModel.TimePeriod = 7;
        }

        /// <summary>
        /// Change the time period to 30 days
        /// </summary>
        /// <param name="sender">the ApplicationBarMenuItem</param>
        /// <param name="e">the event arguments</param>
        private void ThirtyDays_Click(object sender, EventArgs e)
        {
            ////this.OneDay.Text = "1 Day";
            ////this.SevenDays.Text = "7 Days";
            ////this.ThirtyDays.Text = "● 30 Days";
            App.ViewModel.TimePeriod = 30;
        }
    }
}